//一个航班预定程序，有较为完整的用户输入测试程序和完善的交互界面。
//座位表的命名规则是：G+作为的顺序
#include <stdio.h>
#include <ctype.h>
#define SIZE 12
int occu = 0;
void welcom_print(void);
char judge_char(void);
void eatline(void);
void show_number(void);
void show_list_empty(void);
void show_list_alpha(void);
void assign(void);
void delete_data(void);
void sign_assign_brige_system(int num);
void seat_have_reload(void);//对seat_have.oucupied进行初始化，并且对座位编号进行初始化
struct flight
{
	char number[2];
	int oucupied;
	char ming[10];
	char xing[10];
	int hide_number;
};
struct flight seat_have[12];
int main(void)
{
	seat_have_reload();
	welcom_print();
	char judge;
	while ((judge =getchar()) != 'f')
	{
		switch (judge)
		{
		case 'a':
			show_number();
			break;
		case 'b':
			show_list_empty();
			break;
		case'c':
			show_list_alpha();
			break;
		case 'd':
			assign();
			break;
		case'e':
			delete_data();
			break;
		}
		eatline();
		welcom_print();
	}
	printf("************************DONE************************\n");
}

void welcom_print(void)
{
	printf("***************Giant company***************\n");
	puts("To choose a function,enter its letter label:");
	puts("a)Show number of empty seats");
	puts("b)Show list of empty seats");
	puts("c)Show alphabetical list of seats");
	puts("d)Assign a customer to a seat assignment");
	puts("e)Delete a seat assigment");
	puts("f)Quit");
}
char judge_char(void)
{
	char ch;
	ch = tolower(getchar());
	while ((ch < 'a' && ch>'f') && ch != 'q')
	{
		puts("The characters you enter in is not correct!");
		puts("you can just enter a~f or q to quirt!");
	}
	eatline();
	return ch;
}
void eatline(void)
{
	char ch;
	while ((ch = getchar()) != '\n')
	{
		continue;
	}
}
void show_number(void)
{
	printf("We still have %d seats\n", SIZE - occu);
}
void show_list_empty(void)//输出剩余的座位,使用12个的遍历
{
	int i;
	for (i = 0; i <= SIZE - 1; i++)
	{
		if (seat_have[i].oucupied == 1)
			printf("%3d", seat_have[i].hide_number);
	}
	printf("\n");
}
void show_list_alpha(void)
{
	int i;
	for (i = 0; i <= SIZE - 1; i++)
	{
		if (seat_have[i].oucupied == 1)
			printf("%3s", seat_have[i].number);
		printf("\n");
	}
}
void assign(void) 
{
	int i;
	if (occu != 12)
	{
		printf("You can choose seats as follows:\n");
		show_list_empty();
		printf("Enter the number of seat that you want:");
		scanf_s("%d", &i, 2);
		printf("\n");
		if (i >= 1 && i <= 12)
		{
			if (seat_have[i - 1].oucupied == 1)
				sign_assign_brige_system(i-1);
		}
	}
	else
	{
		puts("Sorry,all seats were occupied!");
	}
}
void sign_assign_brige_system(int num)//这是assign的信息录入桥接文件,num为结构数组的原始值，可以直接食用。
{
	char ming_in[10];
	char xing_in[10];
	int n;
	printf("You are booking the %d seats\n", num + 1);
	puts("There are some informations you need to fill:");
	printf("enter oyur first name:");
	scanf_s("%s", xing_in, 10);
	printf("\n");
	printf("enter oyur last name:");
	scanf_s("%s", ming_in, 10);
	for (n = 0; n <= 9; n++)
	{
		seat_have[num].ming[n] = ming_in[n];
		seat_have[num].xing[n] = xing_in[n];
	}
	seat_have[num].oucupied = 0;
	puts("*****************ticket*****************");
	printf("%s        %s\n", xing_in, ming_in);
	printf("          %s\n", seat_have[num].number);
	occu++;
}
void delete_data(void)
{
	puts("You are delete the data");
	printf("you can delete :");
	int i;
	for (i = 0; i <= SIZE - 1; i++)
	{
		if (seat_have[i].oucupied == 0)
			printf("%3d", seat_have[i].hide_number);
		printf("\n");
	}
	printf("enter the number:");
	int de_num;
	scanf_s("%d", &de_num, 2);
	seat_have[de_num - 1].oucupied = 1;
	occu--;
}
void seat_have_reload(void)//对seat_have.oucupied进行初始化，并且对座位编号进行初始化
{
	int i;
	char mu = 'A';
	for (i = 0; i <= SIZE - 1; i++)
	{
		seat_have[i].oucupied = 1;
		seat_have[i].number[0] = 'G';
		seat_have[i].number[1] = mu;
		seat_have[i].hide_number = i + 1;
		mu++;
	}
}